package com.fqxiny.picture.custom.aop;

import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.fqxiny.picture.custom.manager.auth.model.SpaceUserPermissionConstant;
import com.fqxiny.picture.custom.manager.auth.space.SpaceUserAuthManager;
import com.fqxiny.picture.model.entity.Picture;
import com.fqxiny.picture.model.entity.Space;
import com.fqxiny.picture.model.entity.User;
import com.fqxiny.picture.model.enums.SpaceTypeEnum;
import com.fqxiny.picture.service.PictureService;
import com.fqxiny.picture.service.SpaceService;
import com.fqxiny.picture.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.List;
import java.util.Map;

/**
 * @author : 戚欣扬
 * @code : websocket interceptor
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class WsHandshakeInterceptor implements HandshakeInterceptor {
    private final UserService userService;
    private final PictureService pictureService;
    private final SpaceService spaceService;
    private final SpaceUserAuthManager spaceUserAuthManager;
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
        if(request instanceof ServletServerHttpRequest){
            HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
            String pictureId = servletRequest.getParameter("pictureId");
            if(StrUtil.isBlank(pictureId)){
                log.error("缺少图片参数，拒绝握手");
                return false;
            }
            User loginUser = userService.getLoginUser(servletRequest);
            if(ObjUtil.isEmpty(loginUser)){
                log.error("用户未登录，拒绝握手");
                return false;
            }
            Picture picture = pictureService.getById(pictureId);
            if(picture == null){
                log.error("图片不存在，拒绝握手");
                return false;
            }
            Long spaceId = picture.getSpaceId();
            Space space = null;
            if(spaceId != null){
                space = spaceService.getById(spaceId);
                if(space == null){
                    log.error("空间不存在，拒绝握手");
                    return false;
                }
                if(space.getSpaceType()!= SpaceTypeEnum.TEAM.getValue()){
                    log.info("不是团队空间，拒绝握手");
                    return false;
                }
            }
            List<String> permissionList = spaceUserAuthManager.getPermissionList(space, loginUser);
            if(permissionList.contains(SpaceUserPermissionConstant.PICTURE_EDIT)){
                log.error("没有图片编辑权限，拒绝握手");
                return false;
            }
            attributes.put("user",loginUser);
            attributes.put("userId",loginUser.getId());
            attributes.put("pictureId",Long.valueOf(pictureId));
        }
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {

    }
}


